package com.xoa.service.dutyManagement.impl;


import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.dutyManagement.DutyFormMapper;
import com.xoa.dao.users.UserPrivMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.department.Department;
import com.xoa.model.dutyManagement.DutyForm;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.sms.SmsBody;
import com.xoa.model.users.Users;
import com.xoa.service.department.DepartmentService;
import com.xoa.service.dutyManagement.DutyFormService;
import com.xoa.service.sms.SmsService;
import com.xoa.service.users.UsersPrivService;
import com.xoa.service.users.UsersService;
import com.xoa.util.ExcelUtil;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  值班表业务层
 *  author:殷冬
 *  time:2018-05-12
 *  vision:1.0.0
 */
@Service
public class DutyFormServiceImpl implements DutyFormService {
    @Resource
    private DutyFormMapper dutyFormMapper;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UsersService usersService;

    @Resource
    private UsersPrivService usersPrivService;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private UserPrivMapper userPrivMapper;

    @Resource
    private SmsService smsService;


    /**
     *  添加值班表
     *  author:殷冬
     *  time:2018-05-12
     *  vision:1.0.0
     */
   @Transactional
   public Integer addDutyFormManagement(HttpServletRequest request, DutyForm dutyForm){
       StringBuffer userList = getList(dutyForm);
       Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
       List<Integer> list=departmentMapper.getDeptIdByParent(0);
       //添加一级菜单权限
       int deptYj= getDeptYJ(users.getDeptId(),list);
       dutyForm.setDeptYj(String.valueOf(deptYj));
       Integer in = dutyFormMapper.addDutyFormManagement(dutyForm);

       //向sms_body插入提醒数据
       SmsBody smsBody = new SmsBody();
       smsBody.setFromId(dutyForm.getFormId());
       smsBody.setSmsType("3");
       smsBody.setContent("请查看值班信息");
       smsBody.setSendTime((int)(System.currentTimeMillis() / 1000));
       smsBody.setRemindUrl("/dutyManagement/returnSelDutyFormManagement?dutyId="+ dutyForm.getDutyId());
       smsService.saveSms(smsBody,userList.toString(),"1","0","","","");
       return in;
   }
   //传入信息逻辑处理
   public StringBuffer getList(DutyForm dutyForm){
       //选择人员
       if(dutyForm.getUserId()!=""&&dutyForm.getUserId()!=null){
           String[] userIdList=dutyForm.getUserId().split(",");
           List<String> userArray=new ArrayList();
           for(String userId:userIdList){
               userArray.add(userId);
           }
           StringBuffer userStr=new StringBuffer();
           for(String tempUser:userArray){
               userStr.append(tempUser+",");
           }
           return userStr;
       }
        if(dutyForm.getToId()!=null&&dutyForm.getToId()!=""){
            //部门人员
            List<Users> deptList=new ArrayList();
            if(!StringUtils.checkNull(dutyForm.getToId())){
                deptList=usersService.getUserByDeptIds(dutyForm.getToId(),1);
            }
            List<String> deptArray=new ArrayList();
            for(Users users:deptList){
                deptArray.add(users.getUserId());
            }
            StringBuffer userStr=new StringBuffer();
            for(String tempUser:deptArray){
                userStr.append(tempUser+",");
            }
            return userStr;
        }
        if(dutyForm.getPrivId()!=""&&dutyForm.getPrivId()!=null){
            //角色人员
            List<Users> privList=new ArrayList();
            if(!StringUtils.checkNull(dutyForm.getPrivId())){
                privList=usersService.getUserByDeptIds(dutyForm.getPrivId(),2);
            }
            List<String> privArray=new ArrayList();
            for(Users users:privList){
                privArray.add(users.getUserId());
            }
            StringBuffer userStr=new StringBuffer();
            for(String tempUser:privArray){
                userStr.append(tempUser+",");
            }
            return userStr;
        }
       return null;
   }

   public List<DutyForm> getDutyFormManagementAll(){
       return dutyFormMapper.getDutyFormManagementAll();
   }
    /**
     *  查询值班表
     *  author:殷冬
     *  time:2018-05-12
     *  vision:1.0.0
     */
    public ToJson<DutyForm> getDutyFormManagementList(Map<String,Object> map, Integer page, Integer pageSize, boolean useFlag, Users user, List<DutyForm> list){
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page",pageParams);
        ToJson<DutyForm> toJson = new ToJson<DutyForm>();
        //查询出全部
//        List<DutyForm> list = dutyFormMapper.getDutyFormManagementAll();
        //最终结果
        List<DutyForm> listResult = new ArrayList<DutyForm>();
        //遍历
        for(int i=0;i<list.size();i++){
            //查询出当前登录用户发布的
            if(list.get(i).getFormDept().equals(user.getDeptId())){
                listResult.add(list.get(i));
            }else{
                //获取按照部门发布的
                if(list.get(i).getToId()!="" &&list.get(i).getToId()!=null){
                    //解析部门id串
                    String[] toIdList = list.get(i).getToId().split(",");
                    for(String str :toIdList){
                        if(str!=""&&Integer.parseInt(str)==user.getDeptId()){
                            listResult.add(list.get(i));
                        }
                    }
                }else if(list.get(i).getPrivId()!=""&&list.get(i).getPrivId()!=null){//按照角色获取
                    //解析角色串
                    String[] privIdList = list.get(i).getPrivId().split(",");
                    for(String str:privIdList){
                        if(str!=""&&Integer.parseInt(str)==user.getUserPriv()){
                            listResult.add(list.get(i));
                        }
                    }
                }else if(list.get(i).getUserId()!=""&&list.get(i).getUserId()!=null){//按照人员
                    String[] userIdList = list.get(i).getUserId().split(",");
                    for(String str:userIdList){
                        if(str!=""&&user.getUserId().equals(str)){
                            listResult.add(list.get(i));
                        }
                    }
                }else{//全都为空的直接显示
                    listResult.add(list.get(i));
                }
            }
        }
        //去重
        for (int i = 0; i < listResult.size() - 1; i++) {
            for (int j = listResult.size() - 1; j > i; j--) {
                if (listResult.get(j).getDutyId().equals(listResult.get(i).getDutyId())) {
                    listResult.remove(j);
                }
            }
        }
        Integer[] str = new Integer[listResult.size()];
        for(int i=0;i<listResult.size();i++){
            str[i] = listResult.get(i).getDutyId();
        }
        Map<String,Object> map1 = new HashMap<>();
        map.put("ids",str);
        map1.put("ids",str);
        //查询出全部
        List<DutyForm> list1 = dutyFormMapper.getDutyFormManagementByAllId(map);
        Integer count = dutyFormMapper.getDutyFormManagementByAll(map1);
        pageParams.setTotal(count);


        String toIdStr = "";//按照部门发布
        String privIdStr="";//按照角色发布
        String userIdStr="";//按照人员发布
        for(DutyForm dutyForm1 : list1){
            if (dutyForm1.getAttachmentId()!=null&&dutyForm1.getAttachmentId()!=null) {
                dutyForm1.setAttachment(GetAttachmentListUtil.returnAttachment(dutyForm1.getAttachmentName(), dutyForm1.getAttachmentId(),"", GetAttachmentListUtil.MODULE_NOTIFY));
            }
            String[] toId = dutyForm1.getToId().split(",");
            for(String str1:toId){
                if(str1!=""&&str1!=null){
                    toIdStr+=getDeptById(Integer.parseInt(str1));
                }
            }
            dutyForm1.setToIdStr(toIdStr);
            toIdStr="";

            String[] privId = dutyForm1.getPrivId().split(",");
            for(String str1:privId){
                if(str1!=""&&str1!=null){
                    privIdStr+=getPrivById(Integer.parseInt(str1));
                }
            }
            dutyForm1.setPrivIdStr(privIdStr);
            privIdStr="";
            String[] userId = dutyForm1.getUserId().split(",");
            for(String str1:userId){
                if(str1!=""&&str1!=null){
                    if(!str1.equals("admin")){
                        userIdStr+=getUserById(Integer.parseInt(str1));
                    }else{
                        userIdStr+="系统管理员,";
                    }
                }
            }
            dutyForm1.setUserIdStr(userIdStr);
            userIdStr="";
        }
        toJson.setMsg("ok");
        toJson.setFlag(0);
        toJson.setObj(list1);
        toJson.setTotleNum(pageParams.getTotal());
        return toJson;
    }
//    public ToJson<DutyForm> getDutyFormManagementList(Map<String,Object> map, Integer page, Integer pageSize,boolean useFlag, Users user){
//        PageParams pageParams = new PageParams();
//        pageParams.setUseFlag(useFlag);
//        pageParams.setPage(page);
//        pageParams.setPageSize(pageSize);
//        map.put("page",pageParams);
//        ToJson<DutyForm> toJson = new ToJson<DutyForm>();
//        //查询出全部
//        List<DutyForm> list = dutyFormMapper.getDutyFormManagement(map);
//        //最终结果
//        List<DutyForm> listResult = new ArrayList<DutyForm>();
//        //遍历
//        for(int i=0;i<list.size();i++){
//            //查询出当前登录用户发布的
//            if(list.get(i).getFormDept().equals(user.getDeptId())){
//                listResult.add(list.get(i));
//            }else{
//                //获取按照部门发布的
//                if(list.get(i).getToId()!="" &&list.get(i).getToId()!=null){
//                    //解析部门id串
//                    String[] toIdList = list.get(i).getToId().split(",");
//                    for(String str :toIdList){
//                        if(str!=""&&Integer.parseInt(str)==user.getDeptId()){
//                            listResult.add(list.get(i));
//                        }
//                    }
//                }else if(list.get(i).getPrivId()!=""&&list.get(i).getPrivId()!=null){//按照角色获取
//                    //解析角色串
//                    String[] privIdList = list.get(i).getPrivId().split(",");
//                    for(String str:privIdList){
//                        if(str!=""&&Integer.parseInt(str)==user.getUserPriv()){
//                            listResult.add(list.get(i));
//                        }
//                    }
//                }else if(list.get(i).getUserId()!=""&&list.get(i).getUserId()!=null){//按照人员
//                    String[] userIdList = list.get(i).getUserId().split(",");
//                    for(String str:userIdList){
//                        if(str!=""&&user.getUserId().equals(str)){
//                            listResult.add(list.get(i));
//                        }
//                    }
//                }else{//全都为空的直接显示
//                    listResult.add(list.get(i));
//                }
//            }
//        }
//        //去重
//        for (int i = 0; i < listResult.size() - 1; i++) {
//            for (int j = listResult.size() - 1; j > i; j--) {
//                if (listResult.get(j).getDutyId().equals(listResult.get(i).getDutyId())) {
//                    listResult.remove(j);
//                }
//            }
//        }
//
//        toJson.setObj(listResult);
//        toJson.setTotleNum(pageParams.getTotal());
//        return toJson;
//    }

    /**
     *  查询当前登录用户发布的信息
     *  author:殷冬
     *  time:2018-05-12
     *  vision:1.0.0
     */
    public ToJson<DutyForm> getDutyFormManagementListByUserId(Map<String,Object> map, Integer page, Integer pageSize, boolean useFlag, Users user){
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page",pageParams);
        map.put("formId",user.getUserId());
        ToJson<DutyForm> toJson = new ToJson<DutyForm>();
        //查询出全部
        List<DutyForm> list = dutyFormMapper.getDutyFormManagementListByUserId(map);
        String toIdStr = "";//按照部门发布
        String privIdStr="";//按照角色发布
        String userIdStr="";//按照人员发布
        for(DutyForm dutyForm1 : list){
            if (dutyForm1.getAttachmentId()!=null&&dutyForm1.getAttachmentId()!=null) {
                dutyForm1.setAttachment(GetAttachmentListUtil.returnAttachment(dutyForm1.getAttachmentName(), dutyForm1.getAttachmentId(),"", GetAttachmentListUtil.MODULE_NOTIFY));
            }
            String[] toId = dutyForm1.getToId().split(",");
            for(String str:toId){
                if(str!=""&&str!=null){
                    toIdStr+=getDeptById(Integer.parseInt(str));
                }
            }
            dutyForm1.setToIdStr(toIdStr);
            toIdStr="";

            String[] privId = dutyForm1.getPrivId().split(",");
            for(String str:privId){
                if(str!=""&&str!=null){
                    privIdStr+=getPrivById(Integer.parseInt(str));
                }
            }
            dutyForm1.setPrivIdStr(privIdStr);
            privIdStr="";
            String[] userId = dutyForm1.getUserId().split(",");
            for(String str:userId){
                if(str!=""&&str!=null){
                    if(!str.equals("admin")){
                        userIdStr+=getUserById(Integer.parseInt(str));
                    }else{
                        userIdStr+="系统管理员,";
                    }
                }
            }
            dutyForm1.setUserIdStr(userIdStr);
            userIdStr="";
        }

//        toJson.setMsg("ok");
//        toJson.setFlag(0);
        toJson.setObj(list);
        toJson.setTotleNum(pageParams.getTotal());
        return toJson;
    }
    /**
     * 根据主键id查询
     * author:殷冬
     * time:2018-05-12
     * vision:1.0.0
     */
    public DutyForm getDutyFormById(HttpServletRequest request, Integer dutyId, String sqlType){
        DutyForm dutyForm = dutyFormMapper.getDutyFormById(dutyId);
        //notify.setUsers(null);
//        if (dutyForm.getAttachmentId()!=null&&dutyForm.getAttachmentId()!=null) {
//            dutyForm.setAttachment(GetAttachmentListUtil.returnAttachment(dutyForm.getAttachmentName(), dutyForm.getAttachmentId(), sqlType,GetAttachmentListUtil.MODULE_NOTIFY));
//        }
        //附件处理
        List<Attachment> attachmentList=new ArrayList<Attachment>();
        if(dutyForm.getAttachmentName()!=null&&!"".equals(dutyForm.getAttachmentName())){
            attachmentList= GetAttachmentListUtil.returnAttachment(dutyForm.getAttachmentName(),dutyForm.getAttachmentId(),"xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_NOTIFY);
        }
        dutyForm.setAttachmentList(attachmentList);
        String depId = dutyForm.getToId();
        if(!StringUtils.checkNull(depId)){
            String  depName= departmentService.getDeptNameByDeptId(depId,",");
            if(!"ALL_DEPT".trim().equals(dutyForm.getToId())){
                dutyForm.setToIdStr(depName+",");
            }else{
                dutyForm.setToIdStr(depName);
            }
        }
        String userId = dutyForm.getUserId();
        if(!StringUtils.checkNull(userId)){
            String  userName= usersService.getUserNameById(userId);
            dutyForm.setUserIdStr(userName+",");
        }
        String roleId = dutyForm.getPrivId();
        if(!StringUtils.checkNull(roleId)){
            String  roleName= usersPrivService.getPrivNameByPrivId(roleId,",");
            dutyForm.setPrivIdStr(roleName+",");
        }
        return dutyForm;
    }
    /**
     * 根据主键删除
     * author:殷冬
     * time:2018-05-12
     * vision:1.0.0
     */
    public boolean delDutyFormById(Integer dutyId){
        if(dutyFormMapper.delDutyFormById(dutyId)!=-1){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 根据主键修改
     * author:殷冬
     * time:2018-05-13
     * vision:1.0.0
     */
    public boolean updDutyFormById(DutyForm dutyForm){
        if(dutyFormMapper.updDutyFormById(dutyForm)!=-1){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 删除全部值班表
     * author:殷冬
     * time:2018-05-13
     * vision:1.0.0
     */
    public boolean delDutyFormAll(Users user){
        //根据当前登陆用户查询出可删除的信息主键id
        if(dutyFormMapper.delDutyFormAll()!=-1){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 删除选中的值班信息
     * author:殷冬
     * time:2018-05-13
     * vision:1.0.0
     */
    public boolean delDutyFormByDutyIds(String[] dutyIds){
        if(dutyIds.length>0){
            if(dutyFormMapper.delDutyFormByDutyIds(dutyIds)!=-1){
                return true;
            }else{
                return false;
            }
        }else{
            return true;
        }
    }
    /**
     * 导出表格
     * author:殷冬
     * time:2018-05-13
     * vision:1.0.0
     */
    public List<DutyForm> getDutyFormListRep(Map<String,Object> map,Users user){
        //查询出全部
        List<DutyForm> list = dutyFormMapper.getDutyFormManagement(map);
        //最终结果
        List<DutyForm> listResult = new ArrayList<DutyForm>();
        //遍历
        for(int i=0;i<list.size();i++){
            //获取按照部门发布的
            if(list.get(i).getToId()!="" &&list.get(i).getToId()!=null){
                //解析部门id串
                String[] toIdList = list.get(i).getToId().split(",");
                for(String str :toIdList){
                    if(str!=""&&Integer.parseInt(str)==user.getDeptId()){
                        listResult.add(list.get(i));
                    }
                }
            }else if(list.get(i).getPrivId()!=""&&list.get(i).getPrivId()!=null){//按照角色获取
                //解析角色串
                String[] privIdList = list.get(i).getPrivId().split(",");
                for(String str:privIdList){
                    if(str!=""&&Integer.parseInt(str)==user.getUserPriv()){
                        listResult.add(list.get(i));
                    }
                }
            }else if(list.get(i).getUserId()!=""&&list.get(i).getUserId()!=null){//按照人员
                String[] userIdList = list.get(i).getUserId().split(",");
                for(String str:userIdList){
                    if(str!=""&&user.getUserId().equals(str)){
                        listResult.add(list.get(i));
                    }
                }
            }else{//全都为空的直接显示
                listResult.add(list.get(i));
            }
            //查询出当前登录用户发布的
            if(list.get(i).getFormDept().equals(user.getDeptId())){
                listResult.add(list.get(i));
            }

        }
        //去重
        for (int i = 0; i < listResult.size() - 1; i++) {
            for (int j = listResult.size() - 1; j > i; j--) {
                if (listResult.get(j).getDutyId().equals(listResult.get(i).getDutyId())) {
                    listResult.remove(j);
                }
            }
        }
        return listResult;
    }
    /**
     * 导出表格
     * author:殷冬
     * time:2018-05-13
     * vision:1.0.0
     */
    public List<DutyForm> reportDutyFormByUserId(Map<String,Object> map,Users user){
        map.put("formId",user.getUserId());
        List<DutyForm> list = dutyFormMapper.getDutyFormManagementListByUserId(map);
        return list;
    }
    /**
     *  查询当前登陆用户可查看的值班信息
     *  author:殷冬
     *  time:2018-05-30
     *  vision:1.0.0
     */
    public ToJson<DutyForm> getDutyFormRemind(Map<String,Object> map, Integer page, Integer pageSize, boolean useFlag, String user) {
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page",pageParams);
        map.put("userId",user);
        ToJson<DutyForm> toJson = new ToJson<DutyForm>();
        //查询出全部
//        List<DutyForm> list = dutyFormMapper.getDutyFormManagementListByUserId(map);
        List<DutyForm> list = dutyFormMapper.getDutyFormRemind(map);
//        toJson.setMsg("ok");
//        toJson.setFlag(0);
        toJson.setObj(list);
        toJson.setTotleNum(pageParams.getTotal());
        return toJson;
    }
    public ToJson getDutyFormList(HttpServletRequest request, HttpServletResponse response, int exportId, Integer page, Integer pageSize, boolean useFlag){
        ToJson toJson=new ToJson();
        //得到当前用户
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        //分页
        Map map=new HashMap();
        int count=0;
        List<DutyForm> resultList=new ArrayList();
        try {
                //判断是否有其他参数设置
                if(users.getDeptYj()!=null && !"".equals(users.getDeptYj())){
                    String qx[]=users.getDeptYj().split(",");
                    map.put("deptList",qx);
                    count=dutyFormMapper.getCountdutyformList(map);
                    PageParams pageParams = new PageParams();
                    pageParams.setUseFlag(useFlag);
                    pageParams.setPage(page);
                    pageParams.setPageSize(pageSize);
                    map.put("page",pageParams);
                    if(qx.length>0){
                        //有其他参数设置
                       resultList=dutyFormMapper.getdutyformList(map);
                    }
                }else{
                    //用户只能查看本部门一级
                    int deptId=users.getDeptId();
                    List<String> qxlist=new ArrayList<>();
                    List<Integer> list=departmentMapper.getDeptIdByParent(0);
                    int deptYj=getDeptYJ(deptId,list);
                    String qx[]={String.valueOf(deptYj)};
                    //当前用户的一级
                    map.put("deptList",qx);
                    count=dutyFormMapper.getCountdutyformList(map);
                    PageParams pageParams = new PageParams();
                    pageParams.setUseFlag(useFlag);
                    pageParams.setPage(page);
                    pageParams.setPageSize(pageSize);
                    map.put("page",pageParams);
                    resultList=dutyFormMapper.getdutyformList(map);
                }
            if(resultList!=null &&resultList.size()>0){
                for(DutyForm  dutyForm:resultList){
                   //附件处理
                    List<Attachment> attachmentList=new ArrayList<Attachment>();
                    if(dutyForm.getAttachmentName()!=null&&!"".equals(dutyForm.getAttachmentName())){
                        attachmentList= GetAttachmentListUtil.returnAttachment(dutyForm.getAttachmentName(),dutyForm.getAttachmentId(),"xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_NOTIFY);
                    }
                    dutyForm.setAttachmentList(attachmentList);
                    //处理部门发布范围
                   String toId[]=dutyForm.getToId().split(",");
                   String toIdStr="";
                   if(toId.length!=0&&toId!=null&&toId[0]!=""){
                        for(String toid:toId){
                            toIdStr+= departmentMapper.getDeptNameById(Integer.parseInt(toid))+",";
                        }
                   }
                   dutyForm.setToIdStr(toIdStr);
                   //处理人员发布范围
                    String userId[]=dutyForm.getUserId().split(",");
                    String userIdStr="";
                    if(userId.length!=0&&userId!=null&&userId[0]!=""){
                        for(String userid:userId){
                            userIdStr+= usersMapper.getUsernameByUserId(userid)+",";
                        }
                    }
                    dutyForm.setUserIdStr(userIdStr);

                    //处理角色发布范围
                    String userPrivId[]=dutyForm.getPrivId().split(",");
                    String privIdStr="";
                    if(userPrivId.length!=0&&userPrivId!=null&&userPrivId[0]!=""){
                        for(String userPrivid:userPrivId){
                            privIdStr+= userPrivMapper.getPrivNameById(Integer.valueOf(userPrivid))+",";
                        }
                    }
                    dutyForm.setPrivIdStr(privIdStr);
                }
            }
            toJson.setObj(resultList);
            toJson.setTotleNum(count);
            toJson.setMsg("true");
            //判断导出
            if(exportId ==1){
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("值班信息", 9);
                String[] secondTitles = {"开始时间","结束时间"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"startTime","endTime"};

                HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, resultList, beanProperty);
                ServletOutputStream out = response.getOutputStream();

                String filename = "值班表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }
            toJson.setFlag(0);
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }

        return toJson;
    }



    /**
     *  根据访问路径查询sms_body表主键
     *  author:殷冬
     *  time:2018-05-30
     *  vision:1.0.0
     */
    public  Integer getSmsBodyBodyId(String remindUrl){
        return dutyFormMapper.getSmsBodyBodyId(remindUrl);
    }

    /**
     *  根据用户id和sms_body主键id 修改事务提醒状态为已读
     *  author:殷冬
     *  time:2018-05-30
     *  vision:1.0.0
     */
    public  void updSmsByUserIdAndBodyId(String userId,Integer bodyId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userId",userId);
        map.put("bodyId",bodyId);
        dutyFormMapper.updSmsByUserIdAndBodyId(map);
    }
    //查询当前部门名称
    public String getDeptById(int deptId) {
        Department department = departmentMapper.getDeptById(deptId);
        return department.getDeptName()+",";
    }
    //根据主键id查询当前角色类型名称
    public String getPrivById(int privId){
        String str = dutyFormMapper.getUserPrivById(privId);
        return str+",";
    }
    //根据主键id查询当前人员名字
    public String getUserById(int userId){
        String str = dutyFormMapper.getUserById(userId);
        return str+",";
    }
    //获取当前登录人一级菜单
    public int getDeptYJ(int deptId,List<Integer> list){
        if(deptId == 67711){
            return 67711;
        }else if(deptId!=0){
            for(Integer dep:list){
                if(deptId==dep){
                    return dep;
                }
            }
        }
        int  deparent =departmentMapper.getParenDeptIdBydeptId(deptId);
        return  getDeptYJ(deparent,list);

    }

    //判断是否有查看全部的权限
 /*   public boolean ifCkqb(Users users){

        boolean flag=false;
        //用户
        HierarchicalGlobal hierarchicalGlobal= addressZfwMapper.selectGPerson();
        //部门
        HierarchicalGlobal hierarchicalGlobal1= addressZfwMapper.selectGDept();
        //角色
        HierarchicalGlobal hierarchicalGlobal2= addressZfwMapper.selectGPriv();
        String globalDept = hierarchicalGlobal1.getGlobalDept();
        String globalPerson = hierarchicalGlobal.getGlobalPerson();
        String globalPriv = hierarchicalGlobal2.getGlobalPriv();
        String[] split = globalPerson.split(",");
        String[] split1 = globalDept.split(",");
        String[] split2 = globalPriv.split(",");
        //人员
        if(split.length!=0&&split!=null) {
            for (String splitstr : split) {
                if (users.getUserId().equals(splitstr)) {
                    flag = true;
                }
            }
        }
        //部门
        if(split1.length!=0&&split1!=null) {
            for (String splitstr : split1) {
                if (users.getDeptId().equals(splitstr)) {
                    flag = true;
                }
            }
        }
        //角色
        if(split2.length!=0&&split2!=null) {
            for (String splitstr : split2) {
                if (users.getUserPriv().equals(splitstr)) {
                    flag = true;
                }
            }
        }

        return flag;
    }*/
}
